<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item> 
        </el-breadcrumb>
        <!-- 卡片视图 -->
        <el-card >
            <!-- 搜索 -->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入商品名称" class="input-with-select" v-model="queryInfo.query" clearable @clear="getOrderList1">
                      <el-button slot="append" icon="el-icon-search" @click="getOrderList1"></el-button>
                    </el-input>
                </el-col>
                
            </el-row>
            <!-- 订单展示 -->
            <el-table :data="orderlist" v-loading="loading">
                <el-table-column type="index"></el-table-column>
                <el-table-column label="订单号" prop="orderid"></el-table-column>
                <el-table-column label="订单名称" prop="ordername"></el-table-column>
                <el-table-column label="价格" prop="price"></el-table-column>
                <el-table-column label="年" prop="year"></el-table-column>
                <el-table-column label="月" prop="month"></el-table-column>
                <el-table-column label="状态" prop="state">
                    <template slot-scope="scope">
                        <el-switch
                            v-model="scope.row.state"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                        </el-switch>
                    </template>
                </el-table-column>

            </el-table>
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>


    </div>
</template>

<script>
export default{
    data() {
        var checkId = (rule,value, callback) => {
            const regId = /^[1-9]([0-9]{6})$/
            if(regId.test(value)){
                return callback()
            }
            callback(new Error('ID必须为七位数字'))
        };
        return{
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 10
            },
            orderlist: [
                {
                    orderid: "123456",
                    ordername: "ssss",
                    price: "200",
                    year: "2000",
                    month:"12",
                    state: true 
                }
            ],
            total: 0,
            loading: true,
            addOrderVisible: false,
            editOrderVisible:false,
            addForm: {
                orderid: 1000000,
                    ordername: "",
                    price: 100.00,
                    time: "",
                    state: true , 
            },
            editForm: {
                orderid: 1000000,
                    ordername: "",
                    price: 100.00,
                    time: "",
                    state: true , 
            },
            //^[1-9]\d*$
            addFormRules: {
                ordername: [
                    {required: true, message: '请输入订单名称', trigger: 'blur'},
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
                ],
                orderid: [
                    {validator: checkId , trigger: 'blur' },
                    { required: true, message: '请输入七位订单号', trigger: 'blur' },
                    
                ],
            },
            // showEditDialog(row){
            //     this.editForm = row;
            //     this.editOrderVisible = true;
            // },
            // EditDialogClosed(){
            //     this.getOrderList();
            //     this.editOrderVisible = false;
            // },
        }
    },
    created() {
        this.getOrderList()
    },
    methods: {
        async getOrderList() {
            this.loading = true;
            const {data:res} = await this.$http.get('orders',{ params: this.queryInfo})
            this.orderlist = res.orderlist;
            this.total = res.total;
            this.loading = false;
            console.log(res)
        },
        handleSizeChange(newSize){
            console.log(newSize)
            this.queryInfo.pagesize = newSize
            this.queryInfo.pagenum = 1
            this.getOrderList()
        },
        handleCurrentChange(newPage){
            console.log(newPage)
            this.queryInfo.pagenum = newPage
            this.getOrderList()
        },
        addDialogClosed(){
            this.$refs.addFormRef.resetFields();
        },
        addorder(){
            this.$refs.addFormRef.validate(async valid=>{
                if(!valid) return

               const {data: res} = await this.$http.post('orders', this.addForm)

               if(res.success!== 'yes'){
                   if(res.success === 'id' ){this.$message.error("订单id已存在！")}else{
                       this.$message.error("添加失败！")
                   }
                   
               }else{
                   this.$message.success("添加成功！")
                   this.addOrderVisible = false
                   this.getOrderList1()
               }
            })
            
        },
        async editOrder(){
            const {data:res} = await this.$http.post('orderchange', this.editForm)
            if(res.success!== 'yes'){
                    this.$message.error("编辑失败！")
               }else{
                    this.$message.success("编辑成功！")
                    this.editOrderVisible = false
                    this.getOrderList()
               }

        },
        async changestate(row){
            const {data:res} = await this.$http.get('orderchange',{ params: {orderid:row.orderid,state:row.state}})
            this.getOrderList()
        },
        getOrderList1(){
            this.queryInfo.pagenum=1
            this.getOrderList()
        },
        async removeOrderByid(id){
            // 确认confirm 取消cancel
        const confirmResult = await this.$confirm('此操作将永久删除该订单, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
        }).catch(err => {
            return err
        })
        if(confirmResult !== 'confirm'){
            return this.$message.warning("已取消删除")
        }
        const {data:res} = await this.$http.get('deleteorder',{ params:{orderid: id}})

        if(res.success !== 'yes'){
             return this.$message.error("删除失败")
        }

        this.$message.warning("删除成功")
        this.getOrderList1()
        },
        showEditDialog(row){
                this.editForm = row;
                this.editOrderVisible = true;
            },
            EditDialogClosed(){
                this.getOrderList();
                this.editOrderVisible = false;
            },

    },

}
</script>

<style lang="less" scoped>

</style>
